import 'package:flutter/material.dart';
import 'package:flutter_page_indicator/flutter_page_indicator.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:today_app/bloc/history_today/history_today_model.dart';

import 'history_today_bloc.dart';
import 'history_today_event.dart';
import 'history_today_state.dart';

class HistoryTodayPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => HistoryTodayBloc()..add(InitEvent()),
      child: Builder(builder: (context) => _buildPage(context)),
    );
  }

  Widget _buildPage(BuildContext context) {
    final bloc = BlocProvider.of<HistoryTodayBloc>(context);
    return BlocBuilder<HistoryTodayBloc, HistoryTodayState>(
        bloc: bloc,
        builder: (_, HistoryTodayState state) {
          return Container(
              padding: EdgeInsets.all(12),
              width: MediaQuery.of(_).size.width,
              height: 300,
              child: Swiper(
                itemCount: state.model.result?.length,
                itemBuilder: (_, int index) {
                  List<Result>? results = state.model.result?.cast<Result>();
                  String? title = results?[index].title;
                  String? date = results?[index].date;
                  return ListTile(
                    title: Text(title ?? ""),
                    subtitle: Text(date ?? ""),
                    onTap: (){

                    },
                  );
                },
                indicatorLayout: PageIndicatorLayout.COLOR,
                pagination: SwiperPagination(
                    margin: EdgeInsets.only(bottom: 16),
                    builder: DotSwiperPaginationBuilder(
                        color: Colors.black26,
                        activeColor: Colors.black,
                        space: 6
                    )
                ),
                // controller: SwiperController(),
              )
          );
        }
    );
  }
}

